{% extends "real_estate_app/base.html" %}
{% load i18n adminmedia %}

{% block head %}
<script src="{% admin_media_prefix %}js/jquery.min.js" type="text/javascript"></script>
<script src="{% admin_media_prefix %}js/jquery.init.js" type="text/javascript"></script>
<script src="{{ MEDIA_REAL_ESTATE }}js/fullcalendar/fullcalendar.js"></script>
<script src="{{ MEDIA_REAL_ESTATE }}js/moment.js" ></script>
<link rel="stylesheet" href="{{ MEDIA_REAL_ESTATE }}js/fullcalendar/fullcalendar.css" />
<link rel="stylesheet" href="{{ MEDIA_REAL_ESTATE }}admin/css/visitcalendar/fullcalendar-admin.css" />
{% if not show_events %}
<link rel="stylesheet" href="{{ MEDIA_REAL_ESTATE }}admin/css/visitcalendar/fullcalendar-noevents.css" />
{% endif %}
<link media="print" rel="stylesheet" href="{{ MEDIA_REAL_ESTATE }}js/fullcalendar/fullcalendar.print.css" />

<script type="text/javascript">
(function($) {
	$(document).ready(function() {
		options_plugin={{ fullcalendar_options|safe }};
		options={
					selectable: true,
					{% if add_url %}
						dayClick: function(date, allDay, jsEvent, view) {
							date_format=moment(date).format('YYYY-MM-DD HH:mm:ss')
							{% if show_events %}
								window.location="{{ add_url }}?date_visit="+date_format{% if property %}+"&property_fk="+{{ property.id }}{% endif %}
							{% else %}
									date_format=moment(date).format('YYYY-MM-DD')
									window.location="{% url admin:visitcalendar_visitevent_changelist_day %}"+date_format+'/'
							{% endif %}
							$('#calendar').fullCalendar('unselect');
						},
					{% endif %}
					events: function(tart, end, callback) {
						$.ajax({
	            				url: "{%if property %}{% url visitcalendar-list-json property|slugify %}{% else %}{% url visitcalendar-list-json-index %}{% endif %}{% if admin and not show_events %}?admin=True&no-events=True{% endif %}{% if admin and show_events %}?admin=True{% endif %}",
	            				dataType: 'json',
	            				success: function(doc) {
	            					var events = [];
	            					$(doc).each(function(){
	            						events.push({
	            							title:$(this).attr('title'),
	            							start:$(this).attr('start'),
	            							{% if admin and show_events %}
	            								url_edit:$(this).attr('url'),
	            								url_term:$(this).attr('term'),
	            							{% else %}
	            								url:$(this).attr('url'),
	            							{% endif %}
	            							allDay: false,
	            						});

	            					})
	            					callback(events);
	            				}
				        });
					},
					{% if not show_events %}
						eventRender: function(event, element, view) {
							if (view.name == 'month') {
								day=$.fullCalendar.formatDate(event.start,'yyyy-MM-dd');
								$('td[data-date='+day+']').addClass('fullcalendar-noevents')
								return false
							}
						},
					{% endif %}
					{% if admin and show_events or edit %}
						eventRender: function(event, element) {
	        				element.find(".fc-event-inner")
	        				.prepend($("<span class=\"fc-event-icons\"></span>")
	        				.html("<a title=\"{% trans 'Edit' %}\" href='"+event.url_edit+"'> <img width='13' height='13' src='{{ MEDIA_REAL_ESTATE }}img/pencil.png' /> </a> <a title=\"{% trans 'Print term visit' %}\" href='"+event.url_term+"'> <img width='14' height='14' src='{{ MEDIA_REAL_ESTATE }}img/administrative-docs.png' /></a>" ));
	    				},
    				{% endif %}
		};
		$calendar=$('#{{ html_id }}').fullCalendar(
			$.extend(options_plugin,options)
		);
			$('.fc-button-month').click(function() { $calendar.fullCalendar('refetchEvents')})
			$('.fc-button-agendaWeek').click(function() { $calendar.fullCalendar('refetchEvents')})
			$('.fc-button-agendaDay').click(function() { $calendar.fullCalendar('refetchEvents')})
	})
})(django.jQuery);
</script>
{% endblock %}


{% block content_main %}
	<div id="{{ html_id }}"></div>
{% endblock %}